﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using ud_Utilities;
using ud_Article.Business;

namespace mojoPortal.Web.UI
{
    public partial class ArticleSelectorSetting : UserControl, ISettingControl
    {
        /// <summary>
        /// TinLT: "before ; is ModuleID, after ; is CategoryID".
        /// </summary>
        private string selectedValue = string.Empty;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (HttpContext.Current == null) { return; }
            Load += Page_Load;
            ddlModule.SelectedIndexChanged += ddlModule_SelectedIndexChanged;
            EnsureItems();
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;
            PopulateLabels();
            BindDDLModules();
        }


        private void PopulateLabels()
        {
            ltrModuleChoose.Text = ArticleResources.ModuleChooseLabel;
            ltrCategoryChoose.Text = ArticleResources.CategoryChooseLabel;
        }

        private void EnsureItems()
        {
            //why is this null here, its declared
            if (ddlModule != null) return;
            ddlModule = new DropDownList();
            ddlCategory = new DropDownList();
            if (Controls.Count != 0) return;
            Controls.Add(ddlModule);
            Controls.Add(ddlCategory);
            //BindSelection();
        }

        private void GetSelectedItems()
        {
            selectedValue = string.Empty;
            selectedValue = ddlModule.SelectedValue;
            if (selectedValue == string.Empty) return;
            DataTable dt = Module.GetPageModulesTable(int.Parse(selectedValue));
            if (dt.Rows.Count > 0)
            {
                selectedValue += "-" + dt.Rows[0]["PageID"];
            }
            else
            {
                selectedValue += "--1";
            }
            selectedValue += ";" + ddlCategory.SelectedValue;
        }

        private void BindSelection()
        {
            if (!selectedValue.Equals(string.Empty))
            {
                BindDDLModules();
            }
        }

        void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = sender as DropDownList;
            if (ddl != null) BindDDLCategoriesByModule(int.Parse(ddl.SelectedValue));
        }

        private void BindDDLCategoriesByModule(int moduleID)
        {
            DBArticleCategory repository = new DBArticleCategory();
            ddlCategory.DataSource = repository.GetAll(moduleID);
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataTextField = "Category";
            ddlCategory.DataBind();
            ddlCategory.Items.Insert(0, new ListItem(ArticleResources.DropDownListChooseAll, "0"));

            if (!selectedValue.Equals(string.Empty) && ddlCategory.Items.FindByValue(selectedValue.Remove(0, selectedValue.IndexOf(";") + 1)) != null)
            {
                ddlCategory.Items.FindByValue(selectedValue.Remove(0, selectedValue.IndexOf(";") + 1)).Selected = true;
            }
        }

        private void BindDDLModules()
        {
            //Get Articles Module only
            SiteSettings settings = CacheHelper.GetCurrentSiteSettings();
            DBUtilities repository = new DBUtilities();
            ddlModule.DataSource = repository.GetModuleByModuleGuid(new Guid("7c4ad96d-ef7b-4120-bcb6-1233b87407ef"), settings.SiteId);
            ddlModule.DataTextField = "ModuleTitle";
            ddlModule.DataValueField = "ModuleID";
            ddlModule.DataBind();
            if (ddlModule.Items.Count > 0)
            {
                if (!selectedValue.Equals(string.Empty))
                {
                    ListItem item = ddlModule.Items.FindByValue(selectedValue.Remove(selectedValue.IndexOf("-")));
                    if (item != null)
                    {
                        ddlModule.Items.FindByValue(selectedValue.Remove(selectedValue.IndexOf("-"))).Selected = true;
                    }
                }
                else { ddlModule.SelectedIndex = 0; }
            }
            if (ddlModule.SelectedValue != string.Empty)
            {
                BindDDLCategoriesByModule(int.Parse(ddlModule.SelectedValue));
            }
            for (int i = 0; i < ddlModule.Items.Count; i++)
            {
                ddlModule.Items[i].Text = ddlModule.Items[i].Text.Replace("<span class='second left'>", string.Empty).Replace(
                    "<span class='second right'>", string.Empty).Replace("<span class='first left'>", string.Empty).
                    Replace("<span class='first right'>", string.Empty).Replace("</span>", string.Empty);
            }
        }

        #region ISettingControl

        public string GetValue()
        {
            EnsureItems();
            GetSelectedItems();
            return selectedValue;
        }

        public void SetValue(string val)
        {
            EnsureItems();
            selectedValue = val;
            BindSelection();
        }

        #endregion

    }
}